from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import pymysql
from datetime import datetime

pymysql.install_as_MySQLdb()

app = Flask(__name__)
app.secret_key = '123456'

class Config(object):
    """配置参数"""
    # 设置连接数据库的URL
    user = 'root'
    password = '6541230MSq'
    database = 'doc_ai'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@127.0.0.1:3306/%s' % (user, password, database)

    # 设置sqlalchemy自动更跟踪数据库
    SQLALCHEMY_TRACK_MODIFICATIONS = True

    # 查询时会显示原始SQL语句
    app.config['SQLALCHEMY_ECHO'] = True

    # 禁止自动提交数据处理
    app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = False


# 读取配置
app.config.from_object(Config)

# 创建数据库sqlalchemy工具对象
db = SQLAlchemy(app)

# 用户表
# create table users (
#     id int not null auto_increment,
#     username varchar(255) not null, # 用户名,用于登录
#     nickname varchar(255) not null, # 昵称,用于显示
#     password varchar(255) not null,
#     create_time datetime not null,
#     user_type int not null, # 0: 患者 1: 医生 2: 管理员
#     primary key (id)
# );
class User(db.Model):
    # 定义表名
    __tablename__ = 'users'
    # 定义字段
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(255), unique=True, nullable=False)
    nickname = db.Column(db.String(255), unique=True, nullable=False)
    password = db.Column(db.String(255), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.now)
    user_type = db.Column(db.Integer, default=0)


# 创建药品表
# create table medicine(
#     id int not null auto_increment,
#     name varchar(255) not null,
#     info text not null,
#     primary key (id)
# );
class Medicine(db.Model):
    # 定义表名
    __tablename__ = 'medicine'
    # 定义字段
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(255), unique=True, nullable=False)
    info = db.Column(db.Text, nullable=False)

    def serialize(self):
        return {
            'id': self.id,
            'name': self.name,
            'info': self.info
        }


# 创建疾病表
# create table disease(
#     id int not null auto_increment,
#     name varchar(255) not null,
#     info text not null,
#     primary key (id)
# );
class Disease(db.Model):
    # 定义表名
    __tablename__ = 'disease'
    # 定义字段
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(255), unique=True, nullable=False)
    info = db.Column(db.Text, nullable=False)


# 创建病历表
# create table medical_record(
#     id int not null auto_increment,
#     patient_id int not null,
#     doctor_id int not null,
#     disease_id int not null,
#     info text not null, # 疾病信息
#     treatment text not null, # 治疗方法
#     post_time datetime not null, # 诊断时间
#     end_time datetime not null, # 治疗结束时间
#     primary key (id),
#     foreign key (patient_id) references users(id),
#     foreign key (doctor_id) references users(id)
#     foreign key (disease_id) references disease(id)
# );
class MedicalRecord(db.Model):
    # 定义表名
    __tablename__ = 'medical_record'
    # 定义字段
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    patient_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    doctor_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    disease_id = db.Column(db.Integer, db.ForeignKey('disease.id'), nullable=False)
    info = db.Column(db.Text, nullable=False)
    treatment = db.Column(db.Text, nullable=False)
    post_time = db.Column(db.DateTime, default=datetime.now)
    end_time = db.Column(db.DateTime)


# 创建病历药品表
# create table medical_record_medicine(
#     id int not null auto_increment,
#     medical_record_id int not null,
#     medicine_id int not null,
#     primary key (id),
#     foreign key (medical_record_id) references medical_record(id),
#     foreign key (medicine_id) references medicine(id)
# );
class MedicalRecordMedicine(db.Model):
    # 定义表名
    __tablename__ = 'medical_record_medicine'
    # 定义字段
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    medical_record_id = db.Column(db.Integer, db.ForeignKey('medical_record.id'), nullable=False)
    medicine_id = db.Column(db.Integer, db.ForeignKey('medicine.id'), nullable=False)
    count = db.Column(db.Integer, nullable=False)

# 创建问诊表
# create table interrogate(
#     id int not null auto_increment,
#     user_id int not null,
#     question varchar(255) not null, # question中存取用户发送的信息
#     answer varchar(255) not null, # answer中存储ai返回的信息
#     post_time datetime not null,
#     primary key (id),
#     foreign key (user_id) references users(id)
# );
class Interrogate(db.Model):
    # 定义表名
    __tablename__ = 'interrogate'
    # 定义字段
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    question = db.Column(db.String(255), nullable=False)
    answer = db.Column(db.String(255), nullable=False)
    post_time = db.Column(db.DateTime, default=datetime.now)


if __name__ == '__main__':
    with app.app_context():
        db.drop_all()
        # 创建所有表
        db.create_all()

